<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户" prop="memberid">
      <el-input v-model="dataForm.memberid" placeholder="用户"></el-input>
    </el-form-item>
    <el-form-item label="卡类型" prop="typeid">
      <el-input v-model="dataForm.typeid" placeholder="卡类型"></el-input>
    </el-form-item>
    <el-form-item label="余额" prop="money">
      <el-input v-model="dataForm.money" placeholder="余额"></el-input>
    </el-form-item>
    <el-form-item label="ss余额" prop="ssmoney">
      <el-input v-model="dataForm.ssmoney" placeholder="ss余额"></el-input>
    </el-form-item>
    <el-form-item label="zl余额" prop="zlmoney">
      <el-input v-model="dataForm.zlmoney" placeholder="zl余额"></el-input>
    </el-form-item>
    <el-form-item label="时间" prop="date">
      <el-input v-model="dataForm.date" placeholder="时间"></el-input>
    </el-form-item>
    <el-form-item label="" prop="czjine">
      <el-input v-model="dataForm.czjine" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="beizhu">
      <el-input v-model="dataForm.beizhu" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="" prop="czstatic">
      <el-input v-model="dataForm.czstatic" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          memberid: '',
          typeid: '',
          money: '',
          ssmoney: '',
          zlmoney: '',
          date: '',
          czjine: '',
          beizhu: '',
          czstatic: ''
        },
        dataRule: {
          memberid: [
            { required: true, message: '用户不能为空', trigger: 'blur' }
          ],
          typeid: [
            { required: true, message: '卡类型不能为空', trigger: 'blur' }
          ],
          money: [
            { required: true, message: '余额不能为空', trigger: 'blur' }
          ],
          ssmoney: [
            { required: true, message: 'ss余额不能为空', trigger: 'blur' }
          ],
          zlmoney: [
            { required: true, message: 'zl余额不能为空', trigger: 'blur' }
          ],
          date: [
            { required: true, message: '时间不能为空', trigger: 'blur' }
          ],
          czjine: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          beizhu: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          czstatic: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/jsf/chongzhi/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.memberid = data.chongzhi.memberid
                this.dataForm.typeid = data.chongzhi.typeid
                this.dataForm.money = data.chongzhi.money
                this.dataForm.ssmoney = data.chongzhi.ssmoney
                this.dataForm.zlmoney = data.chongzhi.zlmoney
                this.dataForm.date = data.chongzhi.date
                this.dataForm.czjine = data.chongzhi.czjine
                this.dataForm.beizhu = data.chongzhi.beizhu
                this.dataForm.czstatic = data.chongzhi.czstatic
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/jsf/chongzhi/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'memberid': this.dataForm.memberid,
                'typeid': this.dataForm.typeid,
                'money': this.dataForm.money,
                'ssmoney': this.dataForm.ssmoney,
                'zlmoney': this.dataForm.zlmoney,
                'date': this.dataForm.date,
                'czjine': this.dataForm.czjine,
                'beizhu': this.dataForm.beizhu,
                'czstatic': this.dataForm.czstatic
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
